package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    /**
     *  线索列表
     */
    @Override
    public PageResult<Clue> clueList(ClueDto clueDto) {
        Page<Clue> page = clueMapper.clueList(new Page<Clue>(clueDto.getPage(),clueDto.getPageSize()), clueDto);
       return new PageResult<>(page.getTotal(),page.getRecords());
    }

    /**
     *  根据ID查询线索
     */

    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    /**
     * 跟进线索
     */
    @Transactional (rollbackFor = Exception.class)//事务控制
    @Override
    public void trackRcord(Clue clue) {
        //1.修改基本线索
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //2.添加跟进记录
        ClueTrackRecord clueTrackRecord=new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());// TODO 先写死，后续完善
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setType(1);//正常跟进
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    /**
     *伪线索处理
     */
    @Transactional (rollbackFor = Exception.class)//事务控制
    @Override
    public void falseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        //1.修改基本信息
        Clue clue=new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
         clueMapper.updateById(clue);
        //2.添加伪线索记录
        ClueTrackRecord clueTrackRecord=new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }
    /**
     * 线索转商机
     */
    @Override
    public void toBusiness(Integer id) {
        //1.根据ID查询信息，更改状态
        Clue clue=clueMapper.selectById(id);
        clue.setStatus(5);
       clue.setUpdateTime(LocalDateTime.now());
       clueMapper.updateById(clue);
        //2.拷贝线索信息到商机列表
        Business business=new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId( id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);
    }

    @Override
    public PageResult<Clue> CluePoolList(ClueDto clueDto) {
        Page<Clue> page = clueMapper.CluePoolList(new Page<Clue>(clueDto.getPage(),clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
